# -*- coding: utf-8 -*-

import datetime
import traceback
import time
from scrapy import log
from pymongo import MongoClient
from snowball_crawler.items import  SnowballCubeItem

class mongodbPipeline(object):
    """
       save data to mongodb
    """
    MONGODB_SERVER = "127.0.0.1"
    MONGODB_PORT = 27017
    MONGODB_DB = 'snowball'

    def __init__(self):
        try:
            client = MongoClient(self.MONGODB_SERVER, self.MONGODB_PORT)
            self.db = client[self.MONGODB_DB]
        except Exception as e:
            log.ERROR('ERROR(mongodbPipeline):%s' %(str(e)))
            traceback.print_exc()

    @classmethod
    def from_crawler(cls, crawler):
        cls.MONGODB_SERVER = crawler.settings.get('MONGODB_SERVER', 'localhost')
        cls.MONGODB_PORT = crawler.settings.getint('MONGODB_PORT', 27017)
        cls.MONGODB_DB = crawler.settings.get('MONGODB_DB', 'books_fs')
        pipe = cls()
        pipe.crawler = crawler
        return pipe

    def process_item(self, item, spider):
        # spider.logger.debug('SnowballTrading %s' % item['type'])
         if  isinstance(item, SnowballCubeItem):
            dic = item['cube_info']
            today = time.strftime("%Y-%m-%d", time.localtime())
            curr_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            holding = {
                'owner_friends_count': dic['owner']['friends_count'],
                'owner_followers_count': dic['owner']['followers_count'],
                'owner_status_count': dic['owner']['status_count'],
                'holding': dic['last_success_rebalancing']['holdings'],
                'daily_gain': dic['daily_gain'],
                'total_gain': dic['total_gain'],
                'monthly_gain': dic['monthly_gain'],
                'net_value': dic['net_value'],
                'follower_count': dic['follower_count'],
                'update_time': curr_time
            }
            info= {
                'symbol': dic['symbol'],
                'name': dic['name'],
                'description': dic['description'],
                'market': dic['market'],
                'owner_id': dic['owner_id'],
                'create_date': dic['created_date_format'],
                'active_flag': dic['active_flag'],
                'last_holding': holding,
                'daily_holding' :{today: holding} # 每日持仓和收益统计
                #hist_rebalancing[] 调仓历史
            }

            #daily_holding={

            result= self.db['cubes'].update_one({'symbol': info['symbol']} ,{'$set': info} , True)
            log.msg("Item %s updated to MongoDB database %s/cube_info" % (result, self.MONGODB_DB), level=log.DEBUG,
                    spider=spider)

         return item
